package com.taoge.p4.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taoge.p4.common.entity.Brand;
import com.taoge.p4.common.entity.CategoryBrand;
import com.taoge.p4.common.entity.ProductCategory;
import com.taoge.p4.common.exception.ParamException;
import com.taoge.p4.common.mapper.BrandMapper;
import com.taoge.p4.common.mapper.CategoryBrandMapper;
import com.taoge.p4.common.mapper.ProductCategoryMapper;
import com.taoge.p4.common.params.BrandAddProductCategoryParam;
import com.taoge.p4.common.params.ProductCategoryAddBrandParam;
import com.taoge.p4.common.service.CategoryBrandService;
import com.taoge.p4.common.service.ProductCategoryService;
import com.taoge.p4.common.vo.CategoryBrandVO;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 77052
 * @description 针对表【category_brand(分类品牌关联表)】的数据库操作Service实现
 * @createDate 2025-03-17 14:51:45
 */
@RequiredArgsConstructor
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
        implements CategoryBrandService {

    private final CategoryBrandMapper categoryBrandMapper;

    private final BrandMapper brandMapper;

    private final ProductCategoryMapper productCategoryMapper;

    /**
     * 给品牌添加产品分类关联
     * 例如：品牌：小米，分类：手机，电脑，汽车
     *
     * @param param
     */
    @Override
    public void addBrandCategory(BrandAddProductCategoryParam param) {
        // 查询品牌
        Brand brand = brandMapper.selectById(param.getBrandId());
        if (brand == null) {
            throw new ParamException("未找到品牌");
        }
        // 查询分类列表
        List<ProductCategory> productCategoryList = productCategoryMapper.selectBatchIds(param.getCategoryIdList());
        if (CollectionUtils.isEmpty(productCategoryList)) {
            throw new ParamException("未找到分类");
        }
        List<CategoryBrand> list = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            CategoryBrand categoryBrand = new CategoryBrand();
            categoryBrand.setBrandId(param.getBrandId());
            categoryBrand.setCategoryId(productCategory.getId());
            categoryBrand.setCategoryStatus(productCategory.getStatus());
            categoryBrand.setBrandStatus(brand.getStatus());
            list.add(categoryBrand);
        }
        categoryBrandMapper.saveBatch(list);
    }

    /**
     * 给产品分类添加品牌关联
     * 例如：分类：手机 品牌：小米，华为，苹果
     *
     * @param param
     */
    @Override
    public void addCategoryBrand(ProductCategoryAddBrandParam param) {
        // 查询分类
        ProductCategory productCategory = productCategoryMapper.selectById(param.getCategoryId());
        if (productCategory == null) {
            throw new ParamException("分类不存在");
        }

        // 查询品牌列表
        List<Brand> brandList = brandMapper.selectBatchIds(param.getBrandIdList());
        if (CollectionUtils.isEmpty(brandList)) {
            throw new ParamException("未找到要关联的品牌");
        }
        List<CategoryBrand> list = new ArrayList<>();
        for (Brand brand : brandList) {
            CategoryBrand categoryBrand = new CategoryBrand();
            categoryBrand.setBrandId(brand.getId());
            categoryBrand.setCategoryId(param.getCategoryId());
            categoryBrand.setCategoryStatus(productCategory.getStatus());
            categoryBrand.setBrandStatus(brand.getStatus());
            list.add(categoryBrand);
        }
        categoryBrandMapper.saveBatch(list);
    }

    @Override
    public List<CategoryBrandVO> selectBrandCategory(Long brandId) {
        if (brandId == null) {
            throw new ParamException("请选择要查询的分类");
        }
        //先根据品牌id查询出关联的商品分类id
        List<CategoryBrand> categoryBrands = categoryBrandMapper.selectList(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getBrandId, brandId));
        //获取所有的商品分类id的集合
        List<Long> productCategoryIdList = categoryBrands.stream().map(categoryBrand -> categoryBrand.getCategoryId()).collect(Collectors.toList());
        //如果集合不为空继续操作，为空返回空集合
        if (!CollectionUtils.isEmpty(productCategoryIdList)) {
            //根据商品分类id集合查询出所有的商品分类信息
            List<ProductCategory> productCategories = productCategoryMapper.selectBatchIds(productCategoryIdList);
            Map<Long, ProductCategory> collect = productCategories.stream().collect(Collectors.toMap(ProductCategory::getId, Function.identity()));
//            Map<Long, ProductCategory> collect = new HashMap<>();
//            for (ProductCategory productCategory : productCategories) {
//                collect.put(productCategory.getId(), productCategory);
//            }也可以通过for循环构建Map
            //组装CategoryBrandVO数据返回
            List<CategoryBrandVO> categoryBrandVOList = new ArrayList<>();
            for (CategoryBrand categoryBrand : categoryBrands) {
                CategoryBrandVO categoryBrandVO = CategoryBrandVO.convertFrom(CategoryBrandVO.class, categoryBrand);
                ProductCategory productCategory = collect.get(categoryBrand.getCategoryId());
                categoryBrandVO.setName(productCategory.getName());
                categoryBrandVO.setIcon(productCategory.getIcon());
                categoryBrandVOList.add(categoryBrandVO);
            }
            return categoryBrandVOList;
        }
        return Collections.emptyList();
    }

    @Override
    public List<CategoryBrandVO> selectProductCategory(Long productCategoryId) {
        if (productCategoryId == null) {
            throw new ParamException("请选择要查询的分类");
        }
        List<CategoryBrand> categoryBrands = categoryBrandMapper.selectList(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId,productCategoryId));

        List<Long> brandIdList = categoryBrands.stream().map(categoryBrand -> categoryBrand.getBrandId()).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(brandIdList)) {
            List<Brand> brands = brandMapper.selectBatchIds(brandIdList);
            Map<Long, Brand> collect = brands.stream().collect(Collectors.toMap(Brand::getId, Function.identity()));
            List<CategoryBrandVO> categoryBrandVOList = new ArrayList<>();
            for (CategoryBrand categoryBrand : categoryBrands) {
                CategoryBrandVO categoryBrandVO = CategoryBrandVO.convertFrom(CategoryBrandVO.class, categoryBrand);
                Brand brand = collect.get(categoryBrand.getBrandId());
                categoryBrandVO.setName(brand.getName());
                categoryBrandVO.setIcon(brand.getLogo());
                categoryBrandVOList.add(categoryBrandVO);
            }
            return categoryBrandVOList;
        }
        return Collections.emptyList();
    }
}





